package com.java3.preparedstatement.crud;

import com.java3.bean.Order;
import com.java3.util.JDBCUtils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.*;

/**
 * @author jingjintao
 * @date 2021/6/8 - 23:44
 **/
public class OrderForQuery {

    /*
     * 针对于表的字段名与类的属性名不相同的情况：
     * 1. 必须声明sql时，使用类的属性名来命名字段的别名
     * 2. 使用ResultSetMetaData时，需要使用getColumnLabel()来替换getColumnName(),
     *    获取列的别名。
     *  说明：如果sql中没有给字段其别名，getColumnLabel()获取的就是列名
     *
     *
     */
    @Test
    public void testOrderForQuery(){

        String sql="select order_id orderId,order_name orderName,order_date orderDate from `order` where order_id =?";
        final Order order = orderForQuery(sql, 2);
        System.out.println(order);
    }
    /**
     * 通用的针对于order表的查询操作
     * @return
     */
    public Order orderForQuery(String sql,Object...args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();

            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1,args[i]);
            }
            //执行，获取结果集
            rs = ps.executeQuery();
            //获取结果集的元数据
            ResultSetMetaData rsmd = rs.getMetaData();
            //获取列数
            int columnCount = rsmd.getColumnCount();
            if(rs.next()){
                Order order = new Order();
                for (int i = 0; i < columnCount; i++) {
                    //获取列值
                    final Object columnVal = rs.getObject(i + 1);
                    //通过ResultSetMetaData
                    //获取列的列名：getColumnName() --不推荐使用
                    //获取列的别名：getColumnLabel()
//					String columnName = rsmd.getColumnName(i + 1);
                    final String columnName = rsmd.getColumnLabel(i + 1);
                    final Field field = Order.class.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(order,columnVal);
                }
                return order;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn,ps,rs);
        }
        return null;
    }

    @Test
    public void testQuery1() {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();

            String sql="select order_id,order_name,order_date from `order` where order_id =?";
            ps = conn.prepareStatement(sql);

            ps.setObject(1,1);
            rs = ps.executeQuery();
            if(rs.next()){

                int orderId = (int) rs.getObject(1);
                String orderName = (String) rs.getObject(2);
                Date orderDate = (Date) rs.getObject(3);
                Order order = new Order(orderId,orderName,orderDate);
                System.out.println(order);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn,ps,rs);
        }


    }
}
